package com.lepu.app.fun.index.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.RelativeLayout;

import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragmentActivity;
import com.eyzhs.app.R;
import com.lepu.app.fun.index.fragment.FragmentIndexMore;
import com.lepu.app.fun.my.MySearchActivity;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;

/**
 * 更多每日关注
 * @author wxd
 *
 */
public class IndexMyMoreArticleListActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener,OnTopbarNewRightButtonListener {

	private ViewPager mViewPager = null;
	private RelativeLayout mTabLayoutOne = null;
	private RelativeLayout mTabLayoutTwo = null;

	private View mTopLine1;
	private View mTopLine2;
	private int mCurrentTab = 0;
	
	private int mPeriod = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index_my_more_article_list_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {

		// top bar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setRightButton(R.drawable.index_icon_search_white);
		topbar.setOnTopbarNewRightButtonListener(this);
		
		mPeriod = getIntent().getIntExtra("Period", 1);
		
		topbar.setTopbarTitle(getCurrentTopbarName());
		
		// tab
		mTabLayoutOne = (RelativeLayout) findViewById(R.id.tabLayout1);
		mTabLayoutTwo = (RelativeLayout) findViewById(R.id.tabLayout2);

		mTabLayoutOne.setOnClickListener(mViewClickListener);
		mTabLayoutTwo.setOnClickListener(mViewClickListener);

		mTopLine1 = (View)findViewById(R.id.tabLine1);
		mTopLine2 = (View)findViewById(R.id.tabLine2);
		
		// content
		ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
		FragmentIndexMore tabOne = new FragmentIndexMore();
		FragmentIndexMore tabTwo = new FragmentIndexMore();

		Bundle data1 = new Bundle();
		data1.putInt("Period", mPeriod);
		data1.putInt("SortBy", 1);
		tabOne.setArguments(data1);
		
		Bundle data2 = new Bundle();
		data2.putInt("Period", mPeriod);
		data2.putInt("SortBy", 2);
		tabTwo.setArguments(data2);
		
		fragmentList.add(tabOne);
		fragmentList.add(tabTwo);

		mViewPager = (ViewPager) findViewById(R.id.myViewPager);
		mViewPager.setAdapter(new FragmentPageAdapter(this
				.getSupportFragmentManager(), fragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());
		
		mViewPager.setCurrentItem(0);
		updateTabLine(0);
	}
	
	private String getCurrentTopbarName() {
		String name = "";

		if (mPeriod == 1) {
			name = "备孕";
		} else if (mPeriod == 2) {
			name = "孕期";
		} else if (mPeriod == 3) {
			name = "月子";
		} else {
			name = "育儿";
		}

		return name;
	}

	View.OnClickListener mViewClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tabLayout1:
				if (mCurrentTab == 0)
					return;

				mViewPager.setCurrentItem(0);
				updateTabLine(0);
				break;
			case R.id.tabLayout2:

				if (mCurrentTab == 1)
					return;

				mViewPager.setCurrentItem(1);
				updateTabLine(1);
				break;
			default:
				break;
			}
		}
	};

	private void updateTabLine(int currentIndex) {
		if (currentIndex == 0) {
			mTabLayoutOne.setSelected(true);
			mTabLayoutTwo.setSelected(false);
			mTopLine1.setVisibility(View.VISIBLE);
			mTopLine2.setVisibility(View.INVISIBLE);
		} else {
			mTabLayoutOne.setSelected(false);
			mTabLayoutTwo.setSelected(true);
			mTopLine1.setVisibility(View.INVISIBLE);
			mTopLine2.setVisibility(View.VISIBLE);
		}
	}

	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mCurrentTab = index;
			mViewPager.setCurrentItem(index);
			updateTabLine(index);
		}
	}

	@Override
	public void onTopbarRightButtonSelected() {
		Intent intentSearch = new Intent(this,
				MySearchActivity.class);
		intentSearch.putExtra("type", MySearchActivity.TYPE_ARTICLE);
		startActivity(intentSearch, true);
	}

	
}
